package com.zqj.project.converter;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zqj.project.dto.UserDTO;
import com.zqj.project.entity.UserPO;
import com.zqj.project.model.User;
import com.zqj.project.vo.UserVO;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description: UserConverter
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/12/8 20:53
 * @Version: 1.0
 */
public class UserConverter {

    private UserConverter() {
        throw new IllegalStateException("Utility Class");
    }

    public static UserVO toUserVo(User user) {
        return BeanUtil.toBean(user, UserVO.class);
    }

    public static User toUserModel(UserDTO userDto) {
        return BeanUtil.toBean(userDto, User.class);
    }

    public static User toUserModel(UserPO userPo) {
        return BeanUtil.toBean(userPo, User.class);
    }

    public static UserPO toUserPo(User user) {
        return BeanUtil.toBean(user, UserPO.class);
    }

    public static List<UserVO> toUserVoList(List<User> userModelList) {
        if (CollUtil.isEmpty(userModelList)) {
            return Collections.emptyList();
        }
        return userModelList.stream().map(e -> BeanUtil.toBean(e, UserVO.class)).toList();
    }

    public static List<User> toUserModelList(List<UserPO> userPoList) {
        if (CollUtil.isEmpty(userPoList)) {
            return Collections.emptyList();
        }
        return userPoList.stream().map(e -> BeanUtil.toBean(e, User.class)).toList();
    }

    public static IPage<UserVO> toUserVoPage(IPage<User> userModelPage) {
        IPage<UserVO> userVoPage = new Page<>(userModelPage.getCurrent(), userModelPage.getSize());
        List<UserVO> userVoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(userModelPage.getRecords())) {
            userModelPage.getRecords().forEach(entity -> userVoList.add(BeanUtil.toBean(entity, UserVO.class)));
            userVoPage.setRecords(userVoList);
            userVoPage.setTotal(userModelPage.getTotal());
        }
        return userVoPage;
    }

}